package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    @RequestMapping("/getUsername")
    public Result getUsername(){
        //认证完成后，security框架会将权限认证时查询数据库的user对象存入域对象中，底层实现为session
        //SecurityContextHolder.getContext():就是获取域对象，可以理解为session
        //getAuthentication()：获取认证信息
        //getPrincipal():获取登录后的认证信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user == null){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=null;
        try {
            pageResult= userService.findAll(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }


    /**
     * 保存用户信息
     * @param roleIds 用户拥有的角色id
     * @param user 用户的基本信息
     * @return
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    public Result saveUser(int[] roleIds, @RequestBody com.itheima.pojo.User user){

        try {
            userService.saveUser(roleIds,user);
            return new Result(true, "添加用户信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "添加用户信息失败!!");
    }

    @RequestMapping("/findAllNoPagination")
    @ResponseBody
    public Result findAllNoPagination(){
        try {
            List<Role> list= userService.findAllNoPagination();
            return new Result(true, "查询所有角色成功!",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询所有角色失败!");
    }


    /**
     * 根据id查询用户的基本信息
     * @param id
     * @return
     */
    @RequestMapping("/selectUserById/{id}")
    @ResponseBody
    public Result selectUserById(@PathVariable int id){
        try {
            com.itheima.pojo.User user= userService.selectUserById(id);
            return new Result(true, "查询用户基本信息成功!",user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询用户基本信息失败!");
    }

    /**
     * 根据id查询用户的基本信息
     * @param id
     * @return
     */
    @RequestMapping("/selectMiddleById/{id}")
    @ResponseBody
    public Result selectMiddleById(@PathVariable int id){
        try {
            Integer[] array= userService.selectMiddleById(id);
            return new Result(true, "查询用户角色详情成功!",array);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询用角色信息失败!");
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public Result updateUser(int[] roleIds, @RequestBody com.itheima.pojo.User user){
        try {
            userService.updateUser(roleIds,user);
            return new Result(true, "修改角色信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改角色信息失败!");
    }

    @RequestMapping("/deleteUser/{id}")
    @ResponseBody
    public Result deleteUser(@PathVariable int id){
        try {
            userService.deleteUserById(id);
            return new Result(true, "删除用户成功!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "删除用户信息失败!!");
    }
}
